package com.lin.services.impl;

import com.lin.dao.CategoryDao;
import com.lin.pojo.Category;
import com.lin.services.CategoryService;
import com.lin.utils.ShiroUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * ClassName:CategoryServiceImpl
 * Package:com.lin.services.impl
 * Author: hellcat
 * Date: 2019/12/22
 * ♣♣♣
 */
@Service
public class CategoryServiceImpl implements CategoryService {
    @Autowired
    private CategoryDao categoryDao;

    @Override
    public List<Map<String, Object>> getAllCategory() {
        int storeId = ShiroUtil.getUser().getStoreId();
        List<Category> lists = categoryDao.listCategory(storeId);
        List<Map<String, Object>> resultList = new ArrayList<>();
        for (Category category1 : lists) {
            if (category1.getParentCategoryId() == -1) {
                Map<String, Object> map = new HashMap<>();
                List<Map<String, Object>> childrenList = new ArrayList<>();
                map.put("id", category1.getCategoryId());
                map.put("parentId", category1.getParentCategoryId());
                map.put("last", false);
                map.put("spread", false);
                int totals = 0;
                for (Category category2 : lists) {
                    Map<String, Object> childrenMap = new HashMap<>();
                    if (category2.getParentCategoryId().equals(category1.getCategoryId())) {
                        int total = categoryDao.getTotalByCategoryId(category2);
                        totals += total;
                        childrenMap.put("title", category2.getCategoryName() + "(" + total + ")");
                        childrenMap.put("id", category2.getCategoryId());
                        childrenMap.put("parentId", category2.getParentCategoryId());
                        childrenMap.put("last", true);
                        childrenList.add(childrenMap);
                    }
                }
                map.put("title", category1.getCategoryName() + "(" + totals + ")");
                map.put("children", childrenList);
                resultList.add(map);
            }
        }
        return resultList;
    }

    @Override
    public List<Category> getCategory1() {
        int storeId = ShiroUtil.getUser().getStoreId();
        return categoryDao.listCategory1(storeId);
    }

    @Override
    public Map<String, Object> getCategory2(Integer parentCategoryId) {
        Map<String, Object> map = new HashMap<>();
        try {
            List<Category> categories = categoryDao.listCategory2(parentCategoryId, ShiroUtil.getUser().getStoreId());
            map.put("code", true);
            map.put("category2", categories);
        } catch (Exception e) {
            map.put("code", false);
            map.put("msg", "获取失败");
        }
        return map;
    }

    @Override
    public Map<String, Object> insertCategory(String parentCategoryId, String categoryName) {
        Map<String, Object> map = new HashMap<>();
        map.put("msg", "添加失败");
        map.put("code", false);
        if (parentCategoryId != null && !"".equals(parentCategoryId.trim()) && categoryName != null && !"".equals(categoryName.trim())) {
            Category category = new Category();
            category.setCategoryName(categoryName);
            category.setStoreId(ShiroUtil.getUser().getStoreId());
            category.setParentCategoryId(Integer.parseInt(parentCategoryId));
            List<Category> categoryList =
                    categoryDao.getCategoryByCategoryNameAndParentId(categoryName, parentCategoryId, ShiroUtil.getUser().getStoreId());
            if (!categoryList.isEmpty()) {
                map.put("msg", "分类已存在");
                return map;
            }
            categoryDao.insertCategory(category);
            Integer nodeId = category.getCategoryId();
            if (nodeId != null) {
                map.put("code", true);
                map.put("msg", "添加成功");
                map.put("nodeId", nodeId);
            }
        }

        return map;
    }

    @Override
    public Map<String, Object> updateCategory(String parentCategoryId, String categoryId, String categoryName) {
        Map<String, Object> map = new HashMap<>();
        map.put("code", false);
        map.put("msg", "修改失败");
        if (parentCategoryId != null && !"".equals(parentCategoryId.trim())) {
            if (categoryId != null && !"".equals(categoryId.trim())) {
                if (categoryName != null && !"".equals(categoryName.trim())) {
                    List<Category> categoryList =
                            categoryDao.getCategoryByCategoryNameAndParentId(categoryName, parentCategoryId, ShiroUtil.getUser().getStoreId());
                    if (!categoryList.isEmpty()) {
                        map.put("msg", "分类已存在");
                        return map;
                    }
                    Category category = new Category();
                    category.setParentCategoryId(Integer.parseInt(parentCategoryId));
                    category.setCategoryName(categoryName);
                    category.setCategoryId(Integer.parseInt(categoryId));
                    category.setStoreId(ShiroUtil.getUser().getStoreId());
                    categoryDao.updateCategory(category);
                    map.clear();
                    map.put("code", true);
                    map.put("msg", "修改成功");
                }
            }
        }
        return map;
    }

    @Override
    public Map<String, Object> deleteCategory(String parentCategoryId, String categoryId) {
        Map<String, Object> map = new HashMap<>();
        map.put("code", false);
        map.put("msg", "删除失败");
        if (parentCategoryId != null && !"".equals(parentCategoryId.trim())) {
            if (categoryId != null && !"".equals(categoryId.trim())) {
                Integer parentId = Integer.parseInt(parentCategoryId);
                Integer nodeId = Integer.parseInt(categoryId);
                Category category = new Category();
                category.setParentCategoryId(parentId);
                category.setCategoryId(nodeId);
                category.setStoreId(ShiroUtil.getUser().getStoreId());
                int total = 0;
                if (category.getParentCategoryId() == -1) {
                    List<Category> listCategory2 = categoryDao.listCategory2(category.getCategoryId(), category.getStoreId());
                    for (Category category2 : listCategory2) {
                       category2.setStoreId(category.getStoreId());
                        total += categoryDao.getTotalByCategoryId(category2);
                    }
                } else {
                    total = categoryDao.getTotalByCategoryId(category);
                }
                if (total > 0) {
                    map.put("code", false);
                    map.put("msg", "该分类下任有商品，不可删除");
                    return map;
                }
                categoryDao.deleteCategory(category);
                map.clear();
                map.put("code", true);
            }
        }
        return map;
    }
}
